package com.yzt.message.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.yzt.zhuju.arouter.RouterAccount;
import com.yzt.zhuju.arouter.RouterUser;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.event.RxBus;
import com.yzt.zhuju.event.message.UpdateMessageStatusEvent;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.user.PersonalDetailBasic;
import com.yzt.zhuju.utils.AppDialogUtil;
import com.yzt.zhuju.utils.ViewUtil;
import com.yzt.zhuju.widget.tuiview.TUIImageView;
import com.yzt.p_base.utils.KeyboardUtils;
import com.yzt.p_base.utils.ToastUtil;
import com.yzt.plugin_imageloader.ImageLoadUtil;
import com.yzt.plugin_widget.RoundImageView;
import com.yzt.message.R;

/**
 * author : Irene
 * e-mail : 821970751@qq.com
 * date   : 2020/5/13 14:05
 * desc   : 聊天设置
 */
public class ConversationSettingActivity extends BaseActivity {

    private TUIImageView svToBackList;
    private PersonalDetailBasic mBasicUserInfoResponse;
    private static final String TARGET_INFO = "mBasicUserInfoResponse";
    private static final String SELF_ADDRESS_PRIVILEGE = "selfAddressPrivilege";
    private static final String SELF_ADDRESS_STR = "selfAddressStr";
    private static final String TARGET_UID = "targetUid";
    private String targetUid;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isShouldHideInput(view, ev)) {
                KeyboardUtils.hideSoftInput(mActivity);
                return super.dispatchTouchEvent(ev);
            }
        }
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    /**
     * 判断是否为软键盘和输入框外的区域
     */
    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int top = leftTop[1];
            //点击的是输入框及以下区域，保留点击事件
            return !(event.getY() > top);
        }
        return false;
    }

    public static void startActivity(Activity mActivity, PersonalDetailBasic mBasicUserInfoResponse, String targetUid, boolean selfAddressPrivilege, String selfAddressStr) {
        Intent mIntent = new Intent(mActivity, ConversationSettingActivity.class);
        mIntent.putExtra(TARGET_INFO, mBasicUserInfoResponse);
        mIntent.putExtra(SELF_ADDRESS_PRIVILEGE, selfAddressPrivilege);
        mIntent.putExtra(SELF_ADDRESS_STR, selfAddressStr);
        mIntent.putExtra(TARGET_UID, targetUid);
        mActivity.startActivity(mIntent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_conversation_setting;
    }

    @Override
    public void installViews() {
        mTitleBar.setTitle(R.string.conversation_more_setting);

        mBasicUserInfoResponse = (PersonalDetailBasic) (getIntent().getSerializableExtra(TARGET_INFO));
        if (mBasicUserInfoResponse == null) {
            return;
        }

        targetUid = getIntent().getStringExtra(TARGET_UID);

        // 私聊
        findViewById(R.id.ll_person_info).setOnClickListener(v ->
                RouterUser.PersonalDetailActivity(mBasicUserInfoResponse.getUserId(), "私聊")
        );

        // 举报
        findViewById(R.id.ll_report).setOnClickListener(v -> {
            if (ViewUtil.isFastDoubleClick()) {
                return;
            }
            RouterAccount.ReportActivity(mBasicUserInfoResponse.getUserId());
        });

        RoundImageView rivPhoto = findViewById(R.id.riv_photo);
        int defaultPhoto = mBasicUserInfoResponse.getGender() == 1 ? R.mipmap.icon_default_male_avatar : R.mipmap.icon_default_female_avatar;
        ImageLoadUtil.loadIv(mActivity, mBasicUserInfoResponse.getAvatar(), rivPhoto, defaultPhoto);

        TextView tvName = findViewById(R.id.tv_name);
        tvName.setText(mBasicUserInfoResponse.getNickName());
        tvName.setTextColor(ContextCompat.getColor(this, R.color.color_262628));

        // TODO: 2023/12/20
        svToBackList = findViewById(R.id.sv_to_backlist);
        //  svToBackList.setSelected(mBasicUserInfoResponse.getBlack() == 1);
        svToBackList.setOnClickListener(v -> {
            setBlackList(svToBackList.isSelected());
        });
    }

    /**
     * 黑名单加入或移除
     *
     * @param isChecked 取消    加入
     */
    private void setBlackList(boolean isChecked) {
        int status = isChecked ? 0 : 1;
        if (!isChecked) {
            AppDialogUtil.showNoTitleDialog(mActivity, getString(R.string.add_to_black_list),
                    getString(R.string.cancel),
                    getString(R.string.add_black),
                    new AppDialogUtil.DialogClickInterface() {
                        @Override
                        public void onCancelClick() {
                            svToBackList.setSelected(false);
                        }

                        @Override
                        public void onSureClick() {
                            showMProgress();
                            BizLayer.getInstance().getAccountModule().cancelBlack(mBasicUserInfoResponse.getUserId(),
                                    status, new AresSubscriber() {
                                        @Override
                                        protected void onSuccess(Object o) {
                                            ToastUtil.showMessage(getString(R.string.add_to_black_list_success));
                                            RxBus.getDefault().post(new UpdateMessageStatusEvent(1));
                                            svToBackList.setSelected(true);
                                        }

                                        @Override
                                        protected void onFinallyEnd() {
                                            super.onFinallyEnd();
                                            closeProgress();
                                        }
                                    });
                        }
                    });
        } else {
            showMProgress();
            BizLayer.getInstance().getAccountModule().cancelBlack(mBasicUserInfoResponse.getUserId(), status, new AresSubscriber() {
                @Override
                protected void onSuccess(Object o) {
                    ToastUtil.showMessage(getString(R.string.remove_black_list));
                    RxBus.getDefault().post(new UpdateMessageStatusEvent(0));
                    svToBackList.setSelected(false);
                }

                @Override
                protected void onFinallyEnd() {
                    super.onFinallyEnd();
                    closeProgress();
                }
            });
        }
    }
}
